package com.ruoyi.system.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IShopService;
import com.ruoyi.system.service.IStockLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.StockMapper;
import com.ruoyi.system.service.IStockService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 库存Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-01
 */
@Service
public class StockServiceImpl implements IStockService 
{
    @Autowired
    private StockMapper stockMapper;
    @Autowired
    private IShopService shopService;
    @Autowired
    private IStockLogService stockLogService;
    /**
     * 初始化库存
     *
     * @param data
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult initStock(JSONObject data) {
        long companyId = data.getLong("companyId");
        long shopId = data.getLong("shopId");
        Shop shop = shopService.selectShopById(shopId);
        //出库单明细信息
        List<StockLog> logList = JSON.parseArray(data.getJSONArray("goods").toJSONString(), StockLog.class);
        System.out.println("数据："+logList.size());
        if(logList == null || logList.size() == 0) {return AjaxResult.success();}
        for(StockLog log : logList) {
            log.setShopId(shopId);
            log.setCompanyId(companyId);
            log.setBeforeStock(0.0);
            log.setAfterStock(log.getGoodsNum());
            log.setInOut("1");
            log.setType("5");
            log.setShopName(shop.getShopName());
            log.setCompanyName(shop.getCompanyName());

            int res = stockLogService.insertStockLog(log);
            if(res<0){
                throw new RuntimeException("库存更新失败");
            }
        }
        return AjaxResult.success();
    }

    /**
     * 查询库存
     * 
     * @param id 库存主键
     * @return 库存
     */
    @Override
    public Stock selectStockById(Long id)
    {
        return stockMapper.selectStockById(id);
    }

    /**
     * 查询库存列表
     * 
     * @param stock 库存
     * @return 库存
     */
    @Override
    public List<Stock> selectStockList(Stock stock)
    {
        return stockMapper.selectStockList(stock);
    }

    /**
     * 新增库存
     * 
     * @param stock 库存
     * @return 结果
     */
    @Override
    public int insertStock(Stock stock)
    {
        return stockMapper.insertStock(stock);
    }

    /**
     * 修改库存
     * 
     * @param stock 库存
     * @return 结果
     */
    @Override
    public int updateStock(Stock stock)
    {
        return stockMapper.updateStock(stock);
    }

    /**
     * 批量删除库存
     * 
     * @param ids 需要删除的库存主键
     * @return 结果
     */
    @Override
    public int deleteStockByIds(Long[] ids)
    {
        return stockMapper.deleteStockByIds(ids);
    }

    /**
     * 删除库存信息
     * 
     * @param id 库存主键
     * @return 结果
     */
    @Override
    public int deleteStockById(Long id)
    {
        return stockMapper.deleteStockById(id);
    }
}
